﻿@model List<KRM.Core.vWebDeliveryLocation>
@{
    ViewBag.Title = "Load Maps Location";
    Layout = "~/Views/Shared/_PopupLayout.cshtml";
}

<div class="box" style="padding:10px">
    <div class="box-title" >
        <h4><i class=" icon-truck" style="color: #777"></i> Delivery Location</h4>
    </div>
    <div class="box-content" style="padding: 20px 20px 0px 10px; border: solid 1px gainsboro">
        <form action="/load/DeliveryLocation" method="GET" style="margin-bottom:10px">
            <div class="row-fluid">
                <div class="span1">Load ID</div>
                <div class="span2">
                    <input type="text" class="input-block-level" name="loadid" value="@ViewBag.loadid" />
                </div>
                <div class="span1">OrderRef</div>
                <div class="span2">
                    <input type="text" class="input-block-level" name="orderref"  value="@Request["orderref"]"/>
                </div>
                <div class="span2">Delivery Date</div>
                <div class="span2 input-append datetimepicker">
                    <input data-format="dd/MM/yyyy" type="text" name="deliverydate" style="width: 70%" value="@ViewBag.deliverydate">
                    <span class="add-on">
                        <i data-time-icon="icon-time" data-date-icon="icon-calendar" class="icon-calendar"></i>
                    </span>
                </div>
                <div class="span2">
                    <input type="submit" class="btn btn-primary" value="Search" style="width: 100%" />
                </div>
            </div>
        </form>
        @{
            var lastLocation = Model.LastOrDefault() ?? new KRM.Core.vWebDeliveryLocation();
            <div>
                <div id="map-canvas" style="height: 500px; width: 1148px"></div>
            </div>
            <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCgoFNKXf7DaoHEl8niPdNnEYZnAfRMQDM&sensor=false&v=3.7"></script>
            <script>
                var map;

                function LoadPosition(lat, lng, orderref, loadid, drivername, lastupdated, devicename, deviceuid) {
                    var contentString = '<div id="content" style="width:250px">'
                        + '<h5 style="color:rgb(0, 99, 0);">' + orderref + ' - Delivered</h5>'
                        + '<div id="bodyContent">'
                        + '<p>LoadID: ' + loadid + '</p>'
                        + '<p>Driver: ' + drivername + '</p>'
                        + '<p>Date: ' + lastupdated + '</p>'
                        + '<p>DeviceName: ' + devicename + ' - ' + deviceuid + '</p>'
                        + '</div></div>';

                    var infowindow = new google.maps.InfoWindow({ content: contentString });
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(lat, lng),
                        map: map,
                        title: orderref,
                        icon: "/img/Load/Package-Accept-icon-32.png"
                    });
                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.open(map, marker);
                    });
                    infowindow.open(map, marker);
                }

                function LoadLoadPosition(lat, lng, orderref, loadid, drivername, lastupdated, devicename, deviceuid) {
                    var contentString = '<div id="content" style="width:250px">'
                        + '<h5 style="color: rgb(0, 0, 162);">Load: ' + loadid + ' - Delivering</h5>'
                        + '<div id="bodyContent">'
                        + '<p><b>Orders: ' + orderref + '</b></p>'
                        + '<p>Driver: ' + drivername + '</p>'
                        + '<p>Date: ' + lastupdated + '</p>'
                        + '<p>DeviceName: ' + devicename + ' - ' + deviceuid + '</p>'
                        + '</div></div>';

                    var infowindow = new google.maps.InfoWindow({ content: contentString });
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(lat, lng),
                        map: map,
                        title: loadid + '',
                        icon: "/img/Load/icon_delivery_48.png",
                        animation: google.maps.Animation.DROP
                    });
                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.open(map, marker);
                    });
                    infowindow.open(map, marker);
                }

                $(document).ready(function() {
                    var myLatlng = new google.maps.LatLng(@(lastLocation.Lat ?? 53.185446), @(lastLocation.Lng ?? -0.811975));
                    var mapOptions = {
                        zoom: 10,
                        center: myLatlng
                    };
                    map = new google.maps.Map(document.getElementById('map-canvas'),
                        mapOptions);
                });
            </script>
            foreach (var item in Model.Select(p => p.LoadID).Distinct())
            {
                var dropnumber = 0;
                foreach (var order in Model.Where(p => p.DeliveryStatus != null && p.DeliveryStatus == "DELIVERED" && p.LoadID == item).OrderBy(p => p.LastUpdated))
                {
                    dropnumber++;
                    <script>
                        $(document).ready(function() {
                            LoadPosition(@order.Lat, @order.Lng, '@(dropnumber + " - " + order.OrderRef)', @order.LoadID, '@order.DriverName', '@order.LastUpdated', '@order.DeviceName', '@order.DeviceUUID');
                        });
                    </script>
                }
            }
            foreach (var item in Model.Where(p => p.DeliveryStatus == null).Select(p => p.LoadID).Distinct())
            {
                var loaditems = Model.Where(p => p.DeliveryStatus == null).Where(p => p.LoadID == item).ToList();
                var loadlocation = loaditems.FirstOrDefault(p => p.LoadID == item);
                var orderrefs = loaditems.Aggregate("", (current, order) => current + (" " + order.OrderRef + ","));
                orderrefs = orderrefs.Trim(',');
                <script>
                    $(document).ready(function() {
                        LoadLoadPosition(@loadlocation.Lat, @loadlocation.Lng, '@orderrefs', @loadlocation.LoadID, '@loadlocation.DriverName', '@loadlocation.LastUpdated', '@loadlocation.DeviceName', '@loadlocation.DeviceUUID');
                    });
                </script>
            }
        }
    </div>
</div>

